package xiao.ke.serviceinvoker1.controller;/**
 * @author XuXiaoK
 * @create 2020-01-21 21:15
 */

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import xiao.ke.serviceinvoker1.entity.Person;
import xiao.ke.serviceinvoker1.service.CollapseService;
import xiao.ke.serviceinvoker1.service.PersonService;

import java.awt.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @Author XiaoKe
 * @Date 2020/1/21 21:15
 * @Description TODO
 */
@RestController
@Configuration
public class InvokerController {

    @Autowired
    private PersonService personService;

    @Autowired
    private CollapseService collapseService;

    @GetMapping(value = "/router/{personId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public Person router(@PathVariable Integer personId) {
        for (int i = 0; i < 3; i++) {
            Person p = personService.getPerson(personId);
            System.out.println(i + p.getName());
        }

        return new Person();
    }


    @GetMapping(value = "/collapse", produces = MediaType.APPLICATION_JSON_VALUE)
    public String testCollapse() throws ExecutionException, InterruptedException {
        Future<Person> f1 = collapseService.getSinglePerson(1);
        Future<Person> f2 = collapseService.getSinglePerson(2);
        Future<Person> f3 = collapseService.getSinglePerson(3);
        Person p1 = f1.get();
        Person p2 = f2.get();
        Person p3 = f3.get();
        System.out.println(p1.getId() + "---" + p1.getName());
        System.out.println(p2.getId() + "---" + p2.getName());
        System.out.println(p3.getId() + "---" + p3.getName());
        return "";
    }
}
